package com.vgemv.jsutilitysdk.screenshot;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

import com.vgemv.jsutilitysdk.file.JsFileManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;



public class GajScreenCapturer {
    //截取某一个控件上的内容.
    //该方法只是生成一个和view的尺寸一样的截图,不适合listView等.
    public static String takeShotFromView(View view,String filePath) {

        //        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Bitmap.Config.ARGB_8888);
        //        Canvas canvas = new Canvas(bitmap);
        //        view.draw(canvas);
//        String dirPath = GajFileManager.getAppDocDirPath();
//        String filePath = String.format("%s/screenshot-%d.png",GajFileManager.getAppDocDirPath(),System.currentTimeMillis());
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        //该api会对图像的尺寸进行缩放.
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(),0,0,800,2000);
        if(bitmap != null) {
            File file = new File(filePath);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return filePath;
    }

    //该方法只是生成一个和view的尺寸一样的截图,不适合listView等.
    public static String takeShotFromRecyclerView(RecyclerView view,int height) {

                Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),height,Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                view.draw(canvas);
        String dirPath = JsFileManager.sharedInstance().appDirPathInPubDoc();
        String filePath = String.format("%s/screenshot-%d.png",JsFileManager.sharedInstance().appDirPathInPubDoc(),System.currentTimeMillis());
//        view.setDrawingCacheEnabled(true);
//        view.buildDrawingCache();
        //该api会对图像的尺寸进行缩放.
//        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(),0,0,800,2000);
        if(bitmap != null) {
            File file = new File(filePath);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return filePath;
    }

    //这个方法是,获取每个itemView的数据,并截图,最终是由每个itemView组成的数据.
    //RecyclerView本身的数据没有截取出来.
    public static String takeShotFromRecyclerView2(RecyclerView view) {


        //获取RecyclerView的数据个数.
        int size = view.getAdapter().getItemCount();

        //创建一个Holder? 显示的时候应该已经获取了一个??
        RecyclerView.ViewHolder holder = view.getAdapter().createViewHolder(view, 0);

        //让viewHodler显示第一个view,从而viewHolder中,当前拥有第一个itemView的所有数据.
        view.getAdapter().onBindViewHolder(holder, 0);

        holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());

        //高度为itemvew的高度乘以数量,bitmap用来存储数据.
        Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), holder.itemView.getMeasuredHeight() * size,
                Bitmap.Config.ARGB_8888);
        //操作bitmap的类.
        Canvas bigCanvas = new Canvas(bitmap);
        bigCanvas.drawColor(Color.WHITE);
        Paint paint = new Paint();
        int iHeight = 0;
        //这个应该和下面一样,都可以用循环的方式搞定吧....
        holder.itemView.setDrawingCacheEnabled(true);
        holder.itemView.buildDrawingCache();
        bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
        holder.itemView.setDrawingCacheEnabled(false);
        holder.itemView.destroyDrawingCache();
        iHeight += holder.itemView.getMeasuredHeight();
        for (int i = 1; i < size; i++) {
            //RecyclerView显示指定位置的数据.
            view.getAdapter().onBindViewHolder(holder, i);
            holder.itemView.setDrawingCacheEnabled(true);
            holder.itemView.buildDrawingCache();
            bigCanvas.drawBitmap(holder.itemView.getDrawingCache(), 0f, iHeight, paint);
            iHeight += holder.itemView.getMeasuredHeight();
            holder.itemView.setDrawingCacheEnabled(false);
            holder.itemView.destroyDrawingCache();
        }


        String dirPath = JsFileManager.sharedInstance().appDirPathInPubDoc();
        String filePath = String.format("%s/screenshot-%d.png",dirPath,System.currentTimeMillis());
        if(bitmap != null) {
            File file = new File(filePath);
            FileOutputStream fileOutputStream = null;
            try {
                fileOutputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
        return filePath;
    }
}
